package com.study.system.controller;


import com.study.common.result.Result;
import com.study.model.system.SysPost;
import com.study.model.vo.SysPostQueryVo;
import com.study.system.service.SysPostService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author yubao
 * @since 2022-10-31
 */
@Api(tags = "岗位管理接口")
@RestController
@RequestMapping("/admin/system/sysPost")
public class SysPostController {
    @Autowired
    private SysPostService sysPostService;

    @ApiOperation("更改岗位状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable String id,
                               @PathVariable Integer status) {
        sysPostService.updateStatus(id,status);
        return Result.ok();
    }

    @ApiOperation("岗位列表")
    @GetMapping("/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       SysPostQueryVo sysPostQueryVo) {
        //创建page对象
        Page<SysPost> pageParam = new Page<>(page,limit);
        //调用service方法
        IPage<SysPost> pageModel = sysPostService.selectPage(pageParam,sysPostQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("添加岗位")
    @PostMapping("save")
    public Result save(@RequestBody SysPost post) {
        post.setStatus(1);
        post.setCreateTime(new Date());
        post.setIsDeleted(0);
        boolean is_Success = sysPostService.save(post);
        if(is_Success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("根据id查询")
    @GetMapping("getPost/{id}")
    public Result getUser(@PathVariable String id) {
        SysPost post = sysPostService.getById(id);
        return Result.ok(post);
    }

    @ApiOperation("修改岗位")
    @PostMapping("update")
    public Result update(@RequestBody SysPost post) {
        post.setUpdateTime(new Date());
        boolean is_Success = sysPostService.updateById(post);
        if(is_Success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("删除岗位")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable String id) {
        boolean is_Success = sysPostService.removeById(id);
        if(is_Success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
}

